module ApplicationHelper
  def is_active?(c_name, a_name = nil)
    active = false
    if c_name == controller_name
      if a_name != nil
        active = true if a_name == action_name
      else
        active = true
      end
    end
    active
  end

  def in_active?(c_names)
    c_names.include?(controller_name) ? true : false
  end

  def i18n_enum model, type
    I18n.t "#{model.class.model_name.i18n_key}.#{type}.#{model.send(type)}" if model.respond_to?(type)
  end

  def i18n_global var
    I18n.t "gloabl.#{var}"
  end

  def formattime(time, options = {})
    options[:unit]
    options[:unit] = options[:unit].blank? ? "day" : options[:unit]
    case options[:unit]
    when "day"
      time.to_datetime.strftime('%Y年%m月%d日') if time
    when "noYearOnlyDay"
      time.to_datetime.strftime('%m月%d日') if time
    when "noYear"
      time.to_datetime.strftime('%m月%d日 %H:%M') if time
    when "hour"
      time.to_datetime.strftime('%Y年%m月%d日 %H:%M') if time
    when "noDayOnlyHour"
      time.to_datetime.strftime('%H:%M') if time
    when "numberString"
      time.to_datetime.strftime('%Y%m%d%H%M') if time
    end
  end

  def user_avatar
    current_user.wechat_avatar.blank? ? 'avatar.jpg' : current_user.wechat_avatar
  end

  def notice_message
    flash_messages = []

    close_html = %(<button name="button" type="button" class="close" data-dismiss="alert"></button>)

    flash.each do |type, message|
      type = :success if type.to_sym == :notice
      type = :danger  if type.to_sym == :alert
      text = content_tag(:div, raw(close_html) + message, class: "alert alert-#{type}")
      flash_messages << text if message
    end

    flash_messages.join("\n").html_safe
  end
end
